package com.app.service.Impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.ActivityService;
import com.app.service.AreaService;
import com.app.service.GoodsAdminService;
import com.app.util.CommonHelp;
import com.app.vo.GoodsVo;
import com.app.vo.OrderVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsAdminServiceImpl implements GoodsAdminService {
    @Autowired
    private GoodsAdminMapper goodsAdminMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsExchangeMapper goodsExchangeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserIntegralMapper userIntegralMapper;
    @Autowired
    private AreaService areaService;

    @Override
    public void insert(GoodsModel goods) {

        goods.setCreateTime(CommonHelp.getNowTimestamp());
        goodsAdminMapper.insert(goods);
    }

    @Override
    public void updateById(GoodsModel goods) {
        goods.setUpdateTime(CommonHelp.getNowTimestamp());
        goodsAdminMapper.updateById(goods);
    }

    @Override
    public void deleteGoodsById(Integer id) {
        GoodsModel goods = new GoodsModel();
        goods.setId(id);
        goods.setStates(0);
        goodsAdminMapper.updateById(goods);
    }

    @Override
    public GoodsVo selectGoodsList(Integer page, Integer limit, String name, Integer villageId) {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        List<GoodsModel> goodsModels = goodsAdminMapper.selectGoodsList(page, limit, name, villageId);
        for (int i = 0; i < goodsModels.size(); i++) {
            if (goodsModels.get(i).getVillageId() != 0) {
                AreaModel villageAreaModel = areaService.selectById(goodsModels.get(i).getVillageId());
                AreaModel townAreaModel = areaService.selectById(villageAreaModel.getPid());
                AreaModel cityAreaModel = areaService.selectById(townAreaModel.getPid());
                goodsModels.get(i).setVillageText(cityAreaModel.getName() + "/" + townAreaModel.getName() + "/" + villageAreaModel.getName());
            } else {
                goodsModels.get(i).setVillageText("");
            }
        }
        List<SystemModel> systemModels = goodsMapper.selectTypeList();
        for (int i = 0; i < goodsModels.size(); i++) {
            for (int i1 = 0; i1 < systemModels.size(); i1++) {
                if (goodsModels.get(i).getType().equals(systemModels.get(i1).getId())) {
                    goodsModels.get(i).setTypeText(systemModels.get(i1).getName());
                }
            }
        }
        for (int i = 0; i < goodsModels.size(); i++) {
            if (goodsModels.get(i).getSwiperImage() == null || goodsModels.get(i).getSwiperImage().equals("")) {
                String[] str = new String[0];
                goodsModels.get(i).setSwiperImageArr(str);
            }
            String[] str = goodsModels.get(i).getSwiperImage().split(",");
            goodsModels.get(i).setSwiperImageArr(str);
        }
        Integer count = null;
        QueryWrapper<GoodsModel> queryWrapper = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper.eq("village_id", villageId);
            count = goodsAdminMapper.selectCount(queryWrapper);
        } else {
            count = goodsAdminMapper.selectCount(null);
        }
        GoodsVo vo = new GoodsVo();
        vo.setList(goodsModels);
        vo.setCount(count);
        return vo;

    }

    @Override
    public GoodsModel selectGoodsTitle(Integer villageId) {
        return goodsAdminMapper.selectGoodsTitle(villageId);
    }

    @Override
    public OrderVo selectOrderList(Integer page, Integer limit, String keyword, Integer goodsId, Integer villageId) {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        List<GoodsExchangeModel> goodsExchangeModels = goodsExchangeMapper.selectOrderList(page, limit, goodsId);
        if (!goodsExchangeModels.isEmpty()) {
            for (int i = 0; i < goodsExchangeModels.size(); i++) {
                if (goodsExchangeModels.get(i).getStatus() == 0) {
                    goodsExchangeModels.get(i).setStatusText("待核销");
                }
                if (goodsExchangeModels.get(i).getStatus() == 1) {
                    goodsExchangeModels.get(i).setStatusText("已核销");
                }
                if (goodsExchangeModels.get(i).getStatus() == 2) {
                    goodsExchangeModels.get(i).setStatusText("拒绝");
                }
            }
            for (int i = 0; i < goodsExchangeModels.size(); i++) {
                UserModel userModel = userMapper.selectById(goodsExchangeModels.get(i).getUserId());
                if (userModel != null) {
                    goodsExchangeModels.get(i).setUserName(userModel.getName());
                    goodsExchangeModels.get(i).setPhoneNumber(userModel.getPhoneNumber());
                    goodsExchangeModels.get(i).setAvatar(userModel.getAvatar());
                    AreaModel villageAreaModel = areaService.selectById(userModel.getVillageId());
                    AreaModel townAreaModel = areaService.selectById(villageAreaModel.getPid());
                    AreaModel cityAreaModel = areaService.selectById(townAreaModel.getPid());
                    goodsExchangeModels.get(i).setVillageText(cityAreaModel.getName() + "/" + townAreaModel.getName() + "/" + villageAreaModel.getName());
                }
                GoodsModel goodsModel = goodsAdminMapper.selectById(goodsExchangeModels.get(i).getGoodsId());
                if (goodsModel != null) {
                    goodsExchangeModels.get(i).setGoodsName(goodsModel.getName());
                }
            }
        }
        if (keyword != null) {
            List<GoodsExchangeModel> collect = goodsExchangeModels.stream()
                    .filter(GoodsExchangeModel -> GoodsExchangeModel.getUserName().contains(keyword))
                    .filter(GoodsExchangeModel -> GoodsExchangeModel.getPhoneNumber().contains(keyword))
                    .collect(Collectors.toList());
            if (villageId != null && villageId != 0) {
                List<GoodsExchangeModel> goodsExchangeModelList = new ArrayList<>();
                for (int i = 0; i < collect.size(); i++) {
                    UserModel model1 = userMapper.selectById(collect.get(i).getUserId());
                    if (model1 != null && model1.getVillageId().equals(villageId)) {
                        goodsExchangeModelList.add(collect.get(i));
                    }
                }
                for (int i = 0; i < goodsExchangeModelList.size(); i++) {
                    AreaModel villageAreaModel = areaService.selectById(villageId);
                    AreaModel townAreaModel = areaService.selectById(villageAreaModel.getPid());
                    AreaModel cityAreaModel = areaService.selectById(townAreaModel.getPid());
                    goodsExchangeModelList.get(i).setVillageText(cityAreaModel.getName() + "/" + townAreaModel.getName() + "/" + villageAreaModel.getName());
                }
                OrderVo vo = new OrderVo();
                vo.setList(goodsExchangeModelList);
                vo.setCount(goodsExchangeModelList.size());
                return vo;
            }
            OrderVo vo = new OrderVo();
            vo.setList(collect);
            vo.setCount(collect.size());
            return vo;
        }
        if (villageId != null && villageId != 0) {
            List<GoodsExchangeModel> goodsExchangeModelList = new ArrayList<>();
            for (int i = 0; i < goodsExchangeModels.size(); i++) {
                UserModel model1 = userMapper.selectById(goodsExchangeModels.get(i).getUserId());
                if (model1 != null && model1.getVillageId().equals(villageId)) {
                    goodsExchangeModelList.add(goodsExchangeModels.get(i));
                }
            }
            for (int i = 0; i < goodsExchangeModelList.size(); i++) {
                AreaModel villageAreaModel = areaService.selectById(villageId);
                AreaModel townAreaModel = areaService.selectById(villageAreaModel.getPid());
                AreaModel cityAreaModel = areaService.selectById(townAreaModel.getPid());
                goodsExchangeModelList.get(i).setVillageText(cityAreaModel.getName() + "/" + townAreaModel.getName() + "/" + villageAreaModel.getName());
            }
            OrderVo vo = new OrderVo();
            vo.setList(goodsExchangeModelList);
            vo.setCount(goodsExchangeModelList.size());
            return vo;
        }
        Integer count = goodsExchangeMapper.Count(goodsId);
        OrderVo vo = new OrderVo();
        vo.setList(goodsExchangeModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public GoodsExchangeModel WriteOff(Integer id, Integer status, String exchangeTime) throws ParseException {
        GoodsExchangeModel model = new GoodsExchangeModel();
        model.setId(id);
        model.setStatus(status);
        model.setExchangeTime(exchangeTime);
        goodsExchangeMapper.updateById(model);
        if (status == 0) {
            return model;
        }
        GoodsExchangeModel model1 = goodsExchangeMapper.selectById(id);
        UserModel userModel = userMapper.selectById(model1.getUserId());
        GoodsModel goodsModel = goodsAdminMapper.selectById(model1.getGoodsId());
        QueryWrapper<UserIntegralModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 3);
        queryWrapper.eq("user_id", model1.getUserId());
        queryWrapper.eq("associated_id", model.getId());
        UserIntegralModel userIntegralModel = userIntegralMapper.selectOne(queryWrapper);
        if (status == 2) {
            userIntegralModel.setStatus(2);
            goodsModel.setInventory(goodsModel.getInventory() + model1.getNum());
            if (goodsModel.getInventory() != 0) {
                goodsModel.setStatus(1);
            }
            goodsAdminMapper.updateById(goodsModel);
            userModel.setIntegral(userModel.getIntegral() + userIntegralModel.getIntegral());
            userModel.setUpdateTime(CommonHelp.getNowTimestamp());
            userMapper.updateById(userModel);
            userIntegralModel.setAfterIntegral(userModel.getIntegral());
            userIntegralMapper.updateById(userIntegralModel);
            return model1;
        }
        userIntegralModel.setStatus(1);
        userIntegralModel.setUpdateTime(CommonHelp.getNowTimestamp());
        userIntegralMapper.updateById(userIntegralModel);
        return model1;
    }

    @Override
    public GoodsModel selectGoodsById(Integer id) {
        return goodsAdminMapper.selectById(id);
    }

    @Override
    public GoodsModel selectGoodsInfo(Integer id) {
        GoodsModel goodsModel = goodsAdminMapper.selectById(id);

        if (goodsModel.getSwiperImage() == null || goodsModel.getSwiperImage().equals("")) {
            String[] str = new String[0];
            goodsModel.setSwiperImageArr(str);
        } else {
            String[] str = goodsModel.getSwiperImage().split(",");
            goodsModel.setSwiperImageArr(str);
        }
        return goodsModel;
    }
}
